package server.controller;

import static org.junit.Assert.fail;

import java.awt.Point;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import pantherlot.testing.ACMClone;
import pantherlot.testing.TestingSpots;
import pantherlot.testing.TransparentSpotDisplay;

import static pantherlot.testing.TestUtilities.Wait;
import static pantherlot.testing.TestUtilities.WaitShort;

public class EntranceDisplayControllerTest {

	static ACMClone acm_clone;
	static TestingSpots testing_spots;
	
	static String student_id= "2223432";
	static String faculty_id= "1663314";
	static String handicap_id= "1654333";
	
	public void CloseFrame(JFrame frame)
	{
		frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
	}
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception 
	{
		acm_clone= new ACMClone();
		acm_clone.RunACMClone();
		Wait();
		
		testing_spots= new TestingSpots();
		WaitShort();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception 
	{
		
	}

	@Before
	public void setUp() throws Exception 
	{
		
	}

	@After
	public void tearDown() throws Exception 
	{
		
	}
	
	@Test
	public void testGetDuplicate_001() {
		acm_clone.eDisp.ScanIDAndFinish(student_id);
		WaitShort();
		
		testing_spots.student_spot.GetSpotDisplayFrame().ScanID(student_id);
		WaitShort();
		
		acm_clone.eDisp.GetWelcomeDisplay().ScanID(student_id);
		WaitShort();

		if(!acm_clone.eDisp.getDuplicate())
			fail();
		
		testing_spots.student_spot.sTake.Leave();
		acm_clone.eDisp.GetParkingNotification().ClickNext();
		WaitShort();
	}
	
	@Test
	public void testGetDuplicate_002() {
		acm_clone.eDisp.GetWelcomeDisplay().ScanID(faculty_id);
		WaitShort();

		if(acm_clone.eDisp.getDuplicate())
			fail();
		
		acm_clone.eDisp.GetParkingNotification().ClickCancel();
		WaitShort();
	}
	
	@Test
	public void testIsDuplicate_001() {
		acm_clone.eDisp.ScanIDAndFinish(handicap_id);
		WaitShort();
		
		//acm_clone.eDisp.ScanIDAndFinish(handicap_id);
		//WaitShort();
		
		testing_spots.handicap_spot.sDisp.ScanID(handicap_id);
		WaitShort();
		
		acm_clone.eDisp.GetWelcomeDisplay().ScanID(handicap_id);
		WaitShort();
		
		if(!acm_clone.eDisp.isDuplicate(handicap_id))
			fail("Failed to find duplicate");
		
		testing_spots.handicap_spot.sTake.Leave();
		acm_clone.eDisp.GetParkingNotification().ClickNext();
		WaitShort();
	}
	
	@Test
	public void testIsDuplicate_002() {
		acm_clone.eDisp.GetWelcomeDisplay().ScanID("0");
		WaitShort();
		
		if(acm_clone.eDisp.isDuplicate(student_id))
			fail("Incorrectly determined ID is a duplicate");
		
		acm_clone.eDisp.GetParkingNotification().ClickNext();
		acm_clone.eDisp.GetSpotNumberDisplay().ClickCancel();
		WaitShort();
	}

	@Test
	public void testGetDuplicateParkingSpot_001() {
		acm_clone.eDisp.ScanIDAndFinish(student_id);
		WaitShort();
		
		testing_spots.student_spot.sDisp.ScanID(student_id);
		WaitShort();
		
		acm_clone.eDisp.GetWelcomeDisplay().ScanID(student_id);
		WaitShort();
		
		if(!acm_clone.eDisp.getDuplicateParkingSpot(student_id).getParkingNumber().equals(testing_spots.student_parking_number))
			fail("Failed to find duplicate spot.");
		
		testing_spots.student_spot.sTake.Leave();
		acm_clone.eDisp.GetParkingNotification().ClickNext();
		WaitShort();
	}
	
	@Test
	public void testGetDuplicateParkingSpot_002() {
		acm_clone.eDisp.GetWelcomeDisplay().ScanID(student_id);
		WaitShort();
		
		if(acm_clone.eDisp.getDuplicateParkingSpot(student_id)!= null)
			fail("Incorrectly returned duplicate spot.");
		
		acm_clone.eDisp.GetParkingNotification().ClickNext();
		acm_clone.eDisp.GetSpotNumberDisplay().ClickNext();
		acm_clone.eDisp.GetDisplayDirections().ClickCancel();
		WaitShort();
	}

	@Test
	public void testGetCurrentUserID_001() {
		acm_clone.eDisp.GetWelcomeDisplay().ScanID(handicap_id);
		WaitShort();
		
		if(!acm_clone.eDisp.getCurrentUserID().equals(handicap_id))
			fail();
	}
}
